<?php
/**
 * XIE--Scorpion
 *
 * 用户操作
 */
namespace App\Models\Home;

use DB,Session,Redirect;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{

    //先求出所有的数据;
        public function getcates()
        {
            $data = DB::table("position")->get();
            return $this->nolist($data, 0, 0);
        }

        public function nolist($data, $parent_id = 0, $level = 0)
        {
            static $arr = array();
            foreach ($data as $k => $v) {
                if ($v->parent_id == $parent_id) {
                    $v->level = $level;
                    $arr[] = $v;
                    //求出所有的子分类
                    $this->nolist($data, $v->jt_id, $level + 1);
                }
            }
            return $arr;
        }

    //调用子孙操作
        public function getsun()
        {
            $data = DB::table("position")->get();
            return $this->digui($data, 0);
        }

    //调用递归完成取出所有数据，形成子孙节点的架构
        public function digui($data, $parent_id = 0)
        {
            $child = array();
            foreach ($data as $key => $v) {
                if ($v->parent_id == $parent_id) {
                    $child[] = $v;
                }
            }
            if (empty($child)) {
                return null;
            }
            //取出子级
            foreach ($child as $key => $v) {
                $current_child = $this->digui($data, $v->jt_id);
                if ($current_child) {
                    $child[$key]->child = $current_child;
                }
            }
            return $child;
        }


} 